package com.mingtu.guards.activity;

import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.StringUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.callback.StringDialogCallback;
import com.hjq.gson.factory.GsonFactory;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.ScreenUtils;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.bean.CameraTypeBean;
import com.mingtu.guards.bean.DeptBean;
import com.mingtu.guards.fragment.MonitoringDequiFragment;
import com.mingtu.guards.utils.MyConstant;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MonitoringDequipmentActivity extends MyBaseActivity {


    @BindView(R.id.slide_tab)
    SlidingTabLayout slideTab;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles;
    private int checkPosition;
    private String[] deptNameArray;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_monitoring_dequipment;
    }

    @Override
    protected void initView() {
        super.initView();
        setModuleTitle("监控设备管理");

    }

    @Override
    protected void initData() {
        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
    }

    @Override
    protected void getData() {
        getCameraType();
        getCameraDeptList();
    }

    @Override
    protected void onClickTitlebarRight(View view) {
        super.onClickTitlebarRight(view);
        showDeptPop();
    }

    private class MyPagerAdapter extends LazyFragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        protected Fragment getItem(ViewGroup viewGroup, int position) {
            return mFragments.get(position);
        }
    }

    /**
     * 摄像头类型
     */
    private void getCameraType() {
        OkGo.<String>post(MyConstant.POST_CAMERA_TYPE)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                CameraTypeBean cameraTypeBean = new CameraTypeBean();
                                cameraTypeBean = gson.fromJson(result, CameraTypeBean.class);

                                List<String> typeList = cameraTypeBean.getData();
                                if (typeList != null) {
                                    int size = typeList.size();
                                    if (size > 0) {
                                        mTitles = new String[size];
                                        for (int i = 0; i < size; i++) {
                                            String typeName = typeList.get(i);
                                            mTitles[i] = typeName;
                                            mFragments.add(MonitoringDequiFragment.getInstance(typeName));
                                        }
                                        slideTab.setViewPager(viewPager, mTitles, MonitoringDequipmentActivity.this, mFragments);
                                        slideTab.setOnTabSelectListener(new OnTabSelectListener() {
                                            @Override
                                            public void onTabSelect(int position) {
                                                viewPager.setCurrentItem(position);
                                            }

                                            @Override
                                            public void onTabReselect(int position) {

                                            }
                                        });
                                        viewPager.addOnPageChangeListener(onPageChangeListener);
                                        setTextBold(0);

                                    }
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            setTextBold(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    /**
     * tab选中加粗
     *
     * @param position
     */
    private void setTextBold(int position) {
        for (int i = 0; i < slideTab.getTabCount(); i++) {
            TextView textView = slideTab.getTitleView(i);
            if (position == i) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.sp_15));
                textView.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.sp_14));
                textView.setTypeface(Typeface.DEFAULT);
            }
        }
    }

    /**
     * 获取部门数据
     */
    private void getCameraDeptList() {
        OkGo.<String>post(Constant.getInstance().POST_DEPT_LIST)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                DeptBean deptBean = new DeptBean();
                                deptBean = gson.fromJson(result, DeptBean.class);

                                List<String> deptList = deptBean.getList();
                                if (deptList != null && deptList.size() > 0) {
                                    showTopRightImg(R.mipmap.icon_filte_white2);
                                    deptNameArray = new String[deptList.size() + 1];
                                    deptNameArray[0] = "全部";
                                    for (int i = 0; i < deptList.size(); i++) {
                                        deptNameArray[i + 1] = deptList.get(i);
                                    }
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }

    /**
     * 显示保护区列表
     */
    public void showDeptPop() {
        new XPopup.Builder(ActivityUtil.getTopActivity())
                .hasShadowBg(true)
                .maxHeight(ScreenUtils.getScreenHeight() / 2)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .asCenterList("请选择部门", deptNameArray,
                        null, checkPosition,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                checkPosition = position;
                                EventBus.getDefault().post(new MyEventBus(deptNameArray[checkPosition]));
                            }
                        }).show();

    }

}